package com.example.Bean;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.example.Table.Score;
import com.example.util.HibernateUtil;

@ManagedBean
@SessionScoped
public class ScoreBean {
	private int current_money=0;
	public void setCurrent_money(int current_money) {
		this.current_money = current_money;
	}
	public void setCheck_point(int check_point) {
		this.check_point = check_point;
	}
	private int check_point;
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getCurrent_money() {
		return current_money;
	}
	public int getCheck_point() {
		return check_point;
	}
	public String forward(){
		this.current_money=100;
		return "Score";
	}
	@SuppressWarnings("static-access")
	public void savePersonScore(){
		Session session=new HibernateUtil().getSessionFactory().openSession();
		Transaction tx=session.beginTransaction();
		Score score=new Score();
		score.setName(name);
		score.setScore(current_money);
		session.save(score);
		tx.commit();
		session.close();
		try {
			FacesContext ctx = FacesContext.getCurrentInstance();
			ExternalContext ectx = ctx.getExternalContext();
			HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
			HttpServletResponse response = (HttpServletResponse) ectx
					.getResponse();
			response.sendRedirect(request.getContextPath()
					+ "/faces/index.xhtml");
		} catch (IOException ex) {
		}
	}
}
